Practical No.3: Develop JavaScript to Array functionalities

 Array:

Arrays in JavaScript. In JavaScript, array is a single variable that is used to store different elements. Unlike most languages where array is a reference to the multiple variable, in JavaScript array is a single variable that stores multiple elements.

      1.     The Array object lets you store multiple values in a single variable.

      2.    It stores a fixed-size sequential collection of elements of the same type.

      3.    An array is used to store a collection of data, but it is often more useful an array as a collection of variables of the same type.

Declaring an Array:

In JavaScript the array can be created using Array object. Syntax:

var a = new Array(10);

  In above syntax array is created of 10 elements.

  Using new operator we can allocate the memory dynamically for the arrays. We can also define array by following ways:

var fruits = new Array( "apple", "orange", "mango" );

            Initializing an Array:

  Initializing is the process of assigning a value when either a variable or array is declared.

  While initializing an array:

1.     Declare name of the array

2.    Make use of the keyword new

3.    Each value in array as an array element & separated by comma.


Example: var mylist= new Array(20,40,50,60)


`            Access the Elements of an Array:

Array element can be access by referring to the index number. Syntax: array[index]


        Array functions:

        1. Length: It  will  return  the  length of array. Syntax: array.length();

        2. Add:By using length function, we can add element in array. 

        Syntax: array[array.length];

             3.     Sort: The elements can be sorted using the built in                                function sort. Syntax: array.sort();

            4.    Join: For combining array elements into string we can use join()                                                                                                  function. Syntax: array.join();

            5.    Shift(): Shift method removes the first element from an array and                                                   returns that element. Syntax: array.shift();

            6.    Push(): Push method appends the given element(s) in the last of the                                      arrayand returns the length of the new array. Syntax: array.push(element)       

            7.     Pop(): Pop method removes the last element from an array and returnsthat                 element. Syntax: array.pop();

            8.    Reverse(): Reverse method reverses the element of an array. The first array                                                                             element becomes the last and the last becomes the first.

            Syntax: array.reverse();

        Conclusion:    We  understand  the  different  array functionalities    by    executing JavaScript’s.


        Practical No.3: Develop JavaScript to Array functionalities.

<html>

<body>

<script>

document.write("Array Functionalities :"+ "<br/>"); var a=[20,30,10,60,40];

document.write("1. The Length of array is : " + a.length); document.write("</br>");

document.write("2. Displaying Array Elements:"); for (i=0; i<a.length; i++){

document.write(a[i] + "<br/>");

}

document.write("3. The elements is Added in array:" +"<br>"); a[a.length]=70;

a[a.length]=90;

document.write("Display Array Elements:"); document.write("</br>");

for (i=0; i<a.length; i++){ document.write(a[i] + "<br/>");

}

a.sort();

document.write("4. Display Array Elements After Sorting:"); document.write("</br>");

for (i=0; i<a.length; i++){ document.write(a[i] + "<br/>");

}

var num=a.shift();

document.write("Removed element is : " + num + "<br>"); document.write("5. Dispaly Array element After shift function is : " + "<br>"); for (i=0;i<a.length;i++){

document.write(a[i] + "<br>");

}

var length = a.push(100);

document.write("6. Dispaly Array element After push function is: " + a ); document.write("</br>");

var element = a.pop();

document.write("7. Dispaly Array element After pop function is: " + a ); document.write("</br>");

var arr = a.reverse();

document.write("8. Dispaly Array element After Reverse function is: " + arr );

</script>

</body>

</html>


*********************************OUTPUT**********************

Array Functionalities :

1.  The Length of array is : 5

2. Displaying Array Elements:

20

30

10

60

40

3. The elements is Added in array:

Display Array Elements:

20

30

10

60

40

70

90

4. Display Array Elements After Sorting:

10

20

30

40

60

70

90

Removed element is : 10

5. Dispaly Array element After shift function is :

20

30

40

60

70

90

6. Dispaly Array element After push function is: 20,30,40,60,70,90,100

7.  Dispaly Array element After pop function is: 20,30,40,60,70,90

8. Dispaly Array element After Reverse function is: 90,70,60,40,30,20


Practical No.3: Develop JavaScript to Array functionalities on strings.


<html>

<body>

<script>

document.write("Array Functionalities :"+ "<br/>"); var a=["Abhishek","Akshay","Aniket","Saurabh",]; document.write("<b>The Length of array is :</b> " + a.length);

document.write("</br>"); document.write("<b>Displaying Array Elements:</b><br\>");

for (i=0; i<a.length; i++){ document.write(a[i] + "<br/>");

}

document.write("<b>The elements is Added in array is:</b> Mahesh and Vishal" + "<br>"); a[a.length]="Mahesh";

a[a.length]="Vishal";

document.write("<b>Display Array Elements:</b>"); document.write("</br>");

for (i=0; i<a.length; i++){ document.write(a[i] + "<br/>");

}

a.sort();

document.write("<b>Display Array Elements After Sorting:</b>");

document.write("</br>");

for (i=0; i<a.length; i++){ document.write(a[i] + "<br/>");

}

var num=a.shift();

document.write("<b>Removed element is :</b> " + num

+ "<br>");

document.write("<b>Dispaly Array element After shift function is :</b> " + "<br>");

for (i=0;i<a.length;i++){ document.write(a[i] + "<br>");

}

var length = a.push("Anish"); document.write("<b>Dispaly Array element After push function is:</b> " + a ); document.write("</br>");

var element = a.pop();

document.write("<b>Dispaly Array element After pop


function is:</b> " + a ); document.write("</br>"); var arr = a.reverse(); document.write("<b>Dispaly Array element After Reverse function is:</b>" + arr );

</script>

</body>

</html>

*********************************OUTPUT**********************


Array Functionalities :

The Length of array is : 4

Displaying Array Elements:

                         Abhishek Akshay Aniket Saurabh
                         The elements is Added in array is: Mahesh and Vishal
                         Display Array Elements:
                         Abhishek Akshay Aniket Saurabh Mahesh Vishal
                         Display Array Elements After Sorting:
                         Abhishek Akshay Aniket Mahesh Saurabh Vishal
                         Removed element is : Abhishek
                         Dispaly Array element After push function is:        
                         Akshay,Aniket,Mahesh,Saurabh,Vishal,Anish 
                         Dispaly Array element After pop function is: 
                         Akshay,Aniket,Mahesh,Saurabh,Vishal
                         Dispaly Array element After Reverse function is:
                         Vishal,Saurabh,Mahesh,Aniket,Akshay

Comments